package edu.iul.daam.rap.view.rss;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class VideoParser {

	private final static String begin_video_box_label = "<div class=\"yt-uix-slider-body\">";
	private final static String end_video_box_label = "<div class=\"video-card-footer\">";

	private final static String begin_video = "<div class=\"video-card\">";
	private final static String end_video = "<div class=\"ellipsis\">";
	private final static String begin_title = "<a class=\"video-title ellipsis\" href=\".*?\" title=\".*?\">";
	private final static String end_title = "</a>";
	private final static String begin_link = "<a href=\"/watch?v=";
	private final static String end_link = "\" class=\"ux-thumb-wrap contains-addto \"";
	private final static String begin_view_count = "<span class=\"viewcount\">";
	private final static String end_view_count = "</span>";

	private final static String link_code = "http://www.youtube.com/watch?v=";
	private final static String begin_image_code = "http://i.ytimg.com/vi/";
	private final static String end_image_code = "/mqdefault.jpg";

	private final static String coding = "UTF-8";

	private VideoUnit video;
	private LinkedList<VideoUnit> video_box = new LinkedList<VideoUnit>();



	public LinkedList<VideoUnit> parse (String url) {

		try {

			URL feed_url= new URL(url);
			String page="";
			String page_line;
			InputStreamReader inputSource = new InputStreamReader(feed_url.openStream());
			BufferedReader reader = new BufferedReader(inputSource);

			while (( page_line = reader.readLine()) != null)
				page+=page_line+"\n";

			//            System.out.println(page);
			
			String page_coded = new String (page.getBytes(), coding);
			getVideos(getTop(page_coded));


		} catch (IOException e) {System.err.println(e.getMessage());} 

		return video_box;
	}

	private LinkedList<VideoUnit> getVideos(String top_videos) {

		if(top_videos.contains(begin_video)){
			video = new VideoUnit();

			int begin_index = top_videos.indexOf(begin_video);
			int end_index = top_videos.indexOf(end_video, begin_index);

			/**System.out.println("B "+begin_index);
			System.out.println("E "+end_index);
//			System.out.println(top_videos);**/

			String video_code = top_videos.substring(begin_index+begin_video.length(), end_index);

			String link = getLink(video_code);

			video.setLink(link_code+link);
			video.setImage(begin_image_code+link+end_image_code);
			video.setTitle(getTitle(video_code));
			video.setViewCount(getViewCount(video_code));

			video_box.add(video);

			return getVideos(top_videos.substring(begin_index+begin_video.length()));

		} 

		return video_box;

	}

	private String getViewCount(String video_code) {

		int begin_index = video_code.indexOf(begin_view_count);
		int end_index = video_code.indexOf(end_view_count, begin_index);

		return video_code.substring(begin_index+begin_view_count.length(), end_index);
	}

	private String getTitle(String video_code) {
		
		String video_code_edited="";
		String begin_video_title_edited = "<title>";
		
		Pattern style = Pattern.compile(begin_title);
        Matcher mstyle = style.matcher(video_code);
        
        video_code_edited = mstyle.replaceAll(begin_video_title_edited);
        
//        System.out.println(video_code_edited);
        
		int begin_index = video_code_edited.indexOf(begin_video_title_edited);
		int end_index = video_code_edited.indexOf(end_title, begin_index);
		
	
		/**System.out.println("B "+begin_index);
		System.out.println("E "+end_index);
		System.out.println(video_code);

		//System.out.println(video_code); **/
		
		return video_code_edited.substring(begin_index+begin_video_title_edited.length(), end_index);
	}

	private String getLink(String video_code) {
		int begin_index = video_code.indexOf(begin_link);
		int end_index = video_code.indexOf(end_link, begin_index);

		return video_code.substring(begin_index+begin_link.length(), end_index);
	}

	private String getTop(String page) {
		int begin_index = page.indexOf(begin_video_box_label);
		int end_index = page.indexOf(end_video_box_label, begin_index);

		return page.substring(begin_index+begin_video_box_label.length(), 
				end_index);

	}

}